package com.zywl.web.controller.base;

import com.github.pagehelper.PageInfo;
import com.zywl.core.beans.Log;
import com.zywl.core.beans.User;
import com.zywl.service.LogService;
import com.zywl.utils.models.PageResult;
import com.zywl.utils.utils.AsyncMethodUtil;
import com.zywl.utils.utils.Constants;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class BaseController {
    protected static final Logger logger = Logger.getLogger(BaseController.class);

    @Autowired
    protected AsyncMethodUtil asyncMethodUtil;
    @Autowired
    private LogService iLogService;

    public PageResult putToResult(PageInfo pageInfo) {
        PageResult pageResult = new PageResult("查询成功",pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    /**
     * 日志记录
     *
     * @param request
     * @param logType 日志类型，在LogTypeEnum里
     * @param content
     */
    protected void saveLog(HttpServletRequest request, String logType, String content) {
        User user = (User) request.getSession().getAttribute(Constants.USER_SESSION_KEY);
        if (user != null) {
            Log log = new Log(request, user, logType, content);
            try {
                iLogService.save(log);
            } catch (Exception e) {
                logger.error("记录日志异常", e);
            }
        }
    }
}
